package com.todod.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.todod.common.util.SnowflakeIdGenerator;
import com.todod.mapper.UsersManualCatalogMapper;
import com.todod.model.UsersManualCatalog;
import com.todod.model.UsersManualFile;
import com.todod.service.IUsersManualCatalogService;
import com.todod.service.IUsersManualFileService;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author gsh
 * @since 2020-11-23
 */
@Service
@Transactional
public class UsersManualCatalogServiceImpl extends ServiceImpl<UsersManualCatalogMapper, UsersManualCatalog>
		implements IUsersManualCatalogService {
	@Autowired
	private UsersManualCatalogMapper usersManualCatalogMapper;
	@Autowired
	private IUsersManualFileService usersManualFileService;

	@Override
	public List<UsersManualCatalog> getListByXmId(Long xiangmuId) {

		QueryWrapper<UsersManualCatalog> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("um_xm_id", xiangmuId);
		queryWrapper.orderByAsc("um_sort_num");

		List<UsersManualCatalog> list = usersManualCatalogMapper.selectList(queryWrapper);

		return list;
	}

	@Override
	public void saveData(Long _userId, UsersManualCatalog usersManualCatalog) {

		usersManualCatalog.setId(SnowflakeIdGenerator.getId());

		usersManualCatalog.setCreatetimeTi(LocalDateTime.now());
		usersManualCatalog.setCreateuserId(_userId);
		usersManualCatalogMapper.insert(usersManualCatalog);
	}

	@Override
	public void editData(Long _userId, UsersManualCatalog usersManualCatalog) {

		UsersManualCatalog usersManualCatalog1 = usersManualCatalogMapper.selectById(usersManualCatalog.getId());

		usersManualCatalog.setCreatetimeTi(usersManualCatalog1.getCreatetimeTi());
		usersManualCatalog.setCreateuserId(usersManualCatalog1.getCreateuserId());
		usersManualCatalog.setModifytimeTi(LocalDateTime.now());
		usersManualCatalog.setModifyuserId(_userId);
		usersManualCatalogMapper.updateById(usersManualCatalog);
	}

	@Override
	public Object getInfo(Long code) {
		Map<String, Object> map = new HashMap<>();
		UsersManualCatalog usersManualCatalog = usersManualCatalogMapper.getInfo(code);

		map.put("usersManualCatalog", usersManualCatalog);
		return map;
	}

	@Override
	public List<UsersManualCatalog> getListByParentId(Long xiangmuId) {

		QueryWrapper<UsersManualCatalog> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("um_parent_id", xiangmuId);
		queryWrapper.orderByAsc("um_sort_num");

		List<UsersManualCatalog> list = usersManualCatalogMapper.selectList(queryWrapper);

		return list;
	}

	@Override
	public List<UsersManualCatalog> getListByUserIdAndType(Long _userId, Long xmId) {
		List<UsersManualCatalog> returnList = new ArrayList<UsersManualCatalog>();

		UsersManualCatalog usersManualCatalog = new UsersManualCatalog();
		usersManualCatalog.setId(xmId);
		usersManualCatalog.setParentId(-1L);
		usersManualCatalog.setSortNum(0);
		usersManualCatalog.setName("用户手册目录");
		usersManualCatalog.setFlag("ML");
		usersManualCatalog.setXmId(xmId);

		returnList.add(usersManualCatalog);

		QueryWrapper<UsersManualCatalog> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("um_xm_id", xmId);
		queryWrapper.eq("um_parent_id", xmId);
		queryWrapper.orderByAsc("um_sort_num");

		List<UsersManualCatalog> list = usersManualCatalogMapper.selectList(queryWrapper);

		for (UsersManualCatalog usersManualCatalog1 : list) {
			usersManualCatalog1.setFlag("ML");
			returnList.add(usersManualCatalog1);
		}

		return returnList;
	}

	@Override
	public List<UsersManualCatalog> getListByUserIdAndTypeAndParentId(Long _userId, Long xmId, Long parentId) {
		List<UsersManualCatalog> returnList = new ArrayList<UsersManualCatalog>();

		QueryWrapper<UsersManualCatalog> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("um_xm_id", xmId);
		queryWrapper.eq("um_parent_id", parentId);
		queryWrapper.orderByAsc("um_sort_num");

		List<UsersManualCatalog> list = usersManualCatalogMapper.selectList(queryWrapper);

		if (list != null && list.size() > 0) {
			for (UsersManualCatalog usersManualCatalog : list) {
				usersManualCatalog.setFlag("ML");
				returnList.add(usersManualCatalog);
			}
		} else {// 获取个人创建数据
			List<UsersManualFile> plist = usersManualFileService.getListByClassifyId(parentId);
			for (UsersManualFile usersManualFile : plist) {
				UsersManualCatalog usersManualCatalog = new UsersManualCatalog();
				usersManualCatalog.setId(usersManualFile.getId());
				usersManualCatalog.setParentId(parentId);
				usersManualCatalog.setSortNum(usersManualFile.getSortNum());
				usersManualCatalog.setName(usersManualFile.getTableName());
				usersManualCatalog.setFlag("WD");
				usersManualCatalog.setXmId(xmId);

				returnList.add(usersManualCatalog);
			}
		}

		return returnList;
	}

}
